<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="10">
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-row>
            <el-form-item label="流程名">
              <el-input v-model="query.processName"></el-input>
            </el-form-item>
            <el-form-item label="流程状态">
              <el-select v-model="query.available">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item>
              <el-button type="primary" :loading="queryLoading" @click="getPage()">查询</el-button>
              <el-button @click="reset">重置</el-button>
              <el-button type="success" @click="showAdd()">新增</el-button>
            </el-form-item>
          </el-row>
        </el-form>
        <el-table
          :data="dataList"
          style="width: 100%;margin-bottom: 20px;"
          border
          :header-cell-style="{textAlign : 'center'}"
          :cell-style="{textAlign : 'center'}"
          @row-click="rowChange"
        >
          <el-table-column
            prop="processCode"
            label="流程编码"
          >
          </el-table-column>
          <el-table-column
            prop="processName"
            label="流程名"
          >
          </el-table-column>
          <el-table-column
            label="流程状态"
          >
            <template slot-scope="{row}">
              <el-switch
                v-model="row.available"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
                @change.stop="statusChange(row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
          >
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click.stop="showBindingExecutor(row.id)">执行人绑定</el-button>
              <el-button type="text" size="small" @click.stop="showView(row.id)">查看</el-button>
              <el-button type="text" size="small" @click.stop="showUpdate(row.id)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          layout="total,prev, pager, next"
          :current-page.sync="page"
          :page-size="pageSize"
          :total="total"
          @current-change="getPage"
        >
        </el-pagination>
      </el-col>
      <el-col :span="14">
        <process-node-list :process-id="processId"/>
      </el-col>
    </el-row>
    <opt-process :dialog-visible.sync="optDialog" :opt-flag="optFlag" :process-id="processId"
                 @refresh="getPage"
    />
    <binding-executor :dialog-visible.sync="bingingExecutorDialog" :process-id="processId"/>
  </div>
</template>

<script>
import processApi from '@/api/common/processApi'
import OptProcess from '@/views/common/process/components/OptProcess.vue'
import ProcessNodeList from '@/views/common/process/components/ProcessNodeList.vue'
import BindingExecutor from '@/views/common/process/components/BindingExecutor.vue'

export default {
  name: 'ProcessList',
  components: { BindingExecutor, ProcessNodeList, OptProcess },
  data() {
    return {
      query: {},
      page: 1,
      pageSize: 10,
      queryLoading: false,
      dataList: [],
      total: 0,
      optDialog: false,
      optFlag: '',
      processId: null,
      bingingExecutorDialog: false
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getPage(pageNum = 1) {
      this.processId = null
      this.queryLoading = true
      this.page = pageNum
      processApi.getPage(this.page, this.pageSize, this.query).then(res => {
        this.dataList = res.data.list
        this.total = res.data.total
      }).finally(() => {
        this.queryLoading = false
      })
    },
    rowChange(row) {
      this.processId = row.id
    },
    statusChange(row) {
      processApi.status(row.id, row.available).then(res => {
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        this.getPage()
      })
    },
    showView(processId) {
      this.optDialog = true
      this.optFlag = 'view'
      this.processId = processId
    },
    showAdd() {
      this.optDialog = true
      this.optFlag = 'add'
      this.processId = null
    },
    showUpdate(processId) {
      this.optDialog = true
      this.optFlag = 'update'
      this.processId = processId
    },
    showBindingExecutor(processId) {
      this.bingingExecutorDialog = true
      this.processId = processId
    },
    reset() {
      this.query = {}
    }
  }
}
</script>

<style scoped>
.el-pagination {
  text-align: right;
}
</style>
